<% add_decidim_page_title(t("decidim.admin.titles.initiatives")) %>
<div class="card" id="initiatives">
  <div class="item_show__header">
    <h1 class="item_show__header-title">
      <%= t("decidim.admin.titles.initiatives") %>

      <% if allowed_to? :export, :initiatives %>
        <%= export_dropdowns(query) %>
      <% end %>
    </h1>
  </div>
  <%= admin_filter_selector(:initiatives) %>

  <% if show_initiative_type_callout? %>
    <% link = link_to(t("button", scope:"decidim.initiatives.admin.index.initiatives_types"), new_initiatives_type_path, class: "button button__sm button__secondary mt-4") %>
    <%= cell("decidim/announcement", t("alert_html", scope:"decidim.initiatives.admin.index.initiatives_types", link:), callout_class: "alert") %>
  <% else %>
    <div class="table-stacked">
      <table class="table-list">
        <thead>
        <tr>
          <th class="!text-left"><%= t("models.initiatives.fields.title", scope: "decidim.admin") %></th>
          <th><%= t("models.initiatives.fields.state", scope: "decidim.admin") %></th>
          <th><%= sort_link(query, :supports_count, t("models.initiatives.fields.supports_count", scope: "decidim.admin"), default_order: :desc) %></th>
          <th><%= sort_link(query, :created_at, t("models.initiatives.fields.created_at", scope: "decidim.admin"), default_order: :desc) %></th>
          <th><%= sort_link(query, :published_at, t("models.initiatives.fields.published_at", scope: "decidim.admin"), default_order: :desc) %></th>
          <th><%= t ".actions_title" %></th>
        </tr>
        </thead>
        <tbody>
        <% @initiatives.each do |initiative| %>
          <tr>
            <td class="!text-left" data-label="<%= t("models.initiatives.fields.title", scope: "decidim.admin") %>">
              <% if allowed_to? :edit, :initiative, initiative: initiative %>
                <%= link_to translated_attribute(initiative.title),
                            decidim_admin_initiatives.edit_initiative_path(initiative.to_param) %>
              <% else %>
                <%= translated_attribute(initiative.title) %>
              <% end %>
            </td>
            <td data-label="<%= t("models.initiatives.fields.state", scope: "decidim.admin") %>"><%= humanize_admin_state initiative.state %></td>
            <td data-label="<%= t("models.initiatives.fields.supports_count", scope: "decidim.admin") %>"><%= initiative.supports_count %>/<%= initiative.scoped_type.supports_required %></td>
            <td class="table-list__date" data-label="<%= t("models.initiatives.fields.created_at", scope: "decidim.admin") %>"><%= l initiative.created_at, format: :short %></td>
            <td class="table-list__date" data-label="<%= t("models.initiatives.fields.published_at", scope: "decidim.admin") %>"><%= initiative.published_at? ? l(initiative.published_at, format: :short) : "" %></td>
            <td class="table-list__actions" data-label="<%= t(".actions_title") %>">
              <button type="button" data-controller="dropdown" data-target="actions-initiative-<%= initiative.id %>" aria-label="<%= t("decidim.admin.actions.actions_label", resource: translated_attribute(initiative.title)) %>">
                <%= icon "more-fill", class: "text-secondary" %>
              </button>

              <div class="inline-block relative">
                <ul id="actions-initiative-<%= initiative.id %>" class="dropdown dropdown__action" aria-hidden="true">
                  <% if allowed_to? :edit, :initiative, initiative: initiative %>
                    <li class="dropdown__item">
                      <%= link_to decidim_admin_initiatives.edit_initiative_path(initiative.to_param), class: "dropdown__button" do %>
                        <%= icon "pencil-line" %>
                        <%= t("actions.edit", scope: "decidim.admin") %>
                      <% end %>
                    </li>
                  <% end %>

                  <hr>

                  <% if allowed_to?(:answer, :initiative, initiative: initiative) %>
                    <li class="dropdown__item">
                      <%= link_to edit_initiative_answer_path(initiative.slug), class: "dropdown__button" do %>
                        <%= icon "chat-1-line" %>
                        <%= t("actions.answer", scope: "decidim.initiatives") %>
                      <% end %>
                    </li>
                  <% else %>
                    <li class="dropdown__item">
                      <div class="dropdown__button-disabled">
                        <%= with_tooltip(t("actions.cannot_answer", scope: "decidim.admin")) do %>
                          <%= icon "chat-1-line", class: "text-gray" %>
                          <span><%= t("actions.answer", scope: "decidim.initiatives") %></span>
                        <% end %>
                      </div>
                    </li>
                  <% end %>

                  <% if allowed_to? :print, :initiative, initiative: initiative %>
                    <li class="dropdown__item">
                      <%= link_to decidim_initiatives.print_initiative_path(initiative, locale: current_locale), class: "dropdown__button", target: "_blank", data: { "external-link": false } do %>
                        <%= icon "printer-line" %>
                        <%= t(".print") %>
                      <% end %>
                    </li>
                  <% end %>

                  <hr>

                  <% if allowed_to? :preview, :initiative, initiative: initiative %>
                    <li class="dropdown__item">
                      <%= link_to decidim_initiatives.initiative_path(initiative.to_param, locale: current_locale), class: "dropdown__button", target: "_blank", data: { "external-link": false } do %>
                        <%= icon "eye-line" %>
                        <%= t(".preview") %>
                      <% end %>
                    </li>
                  <% end %>

                  <% if allowed_to? :read, :share_token, current_participatory_space: initiative %>
                    <li class="dropdown__item">
                      <%= link_to decidim_admin_initiatives.initiative_share_tokens_path(initiative), class: "dropdown__button" do %>
                        <%= icon "share-line" %>
                        <%= t("actions.share_tokens", scope: "decidim.admin") %>
                      <% end %>
                    </li>

                    <hr>
                  <% end %>

                  <% if (link = free_resource_permissions_link(initiative)) %>
                    <li class="dropdown__item">
                      <%= link %>
                    </li>
                  <% end %>
                </ul>
              </div>

            </td>
          </tr>
        <% end %>
        </tbody>
      </table>
    <% end %>
  </div>
</div>
<%= decidim_paginate @initiatives %>
